package com.itboye.textileexpres.top;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.textileexpes.beans.GoodsBean;
import com.itboye.textileexpes.presenter.GoodsPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.util.IsMobileNumPhone;
import com.itboye.textileexpres.widget.EditTextWithDelete;

public class ActivityTypeDetail extends BaseActivity implements Observer ,TextWatcher{
	ListView list;
	MyTypeDetailAdapter adapter;
	ImageView back;
	TextView title;
	GoodsPresenter goodsPresenter;
	EditTextWithDelete key_word;
	TextView search;
	GoodsBean bean;
	GoodsBean goodsBean = new GoodsBean();
	List<GoodsBean.GoodsList> goodsBeanslist = new ArrayList<GoodsBean.GoodsList>();
	RelativeLayout null_search;
	String cateID = "";
	String PropID = "";
	String from = "";

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_type_detail;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		goodsPresenter = new GoodsPresenter(this);
		key_word.setFocusable(true);

		key_word.setFocusableInTouchMode(true);

		key_word.requestFocus();
		bean = new GoodsBean();
		cateID = getIntent().getStringExtra("cateid");
		PropID = getIntent().getStringExtra("proid");
		from = getIntent().getStringExtra("from");
		if ("has_prop".equals(from)) {
			bean.setPage_index("1");
			bean.setPage_size("10");
			bean.setCate_id(cateID);
			bean.setProp_id(PropID);
			goodsPresenter.getInfo(bean);
		}

		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}

		});
		key_word.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				bean = new GoodsBean();
				key_word.setFocusable(true);
				key_word.setFocusableInTouchMode(true);
				bean.setPage_index("1");
				bean.setPage_size("10");
				bean.setUid(IsMobileNumPhone.isUid());
				goodsPresenter.getInfo(bean);
			}
		});
		search.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if ("".equals(key_word.getText() + "")
						|| key_word.getText().length() == 0) {
					toast("请输入关键字");
					return;
				} else {
					showProgressDialog("搜索中", true);
					bean.setKeyword(key_word.getText() + "");
					bean.setPage_index("1");
					bean.setPage_size("10");
					bean.setCate_id(cateID);
					bean.setProp_id(PropID);
					bean.setUid(IsMobileNumPhone.isUid());
					goodsPresenter.getInfo(bean);
				}

			}
		});
	}

	class MyTypeDetailAdapter extends BaseAdapter {
		Context context;
		List<GoodsBean.GoodsList> goodsBeanslist;

		public MyTypeDetailAdapter(Context context,
				List<GoodsBean.GoodsList> goodsBeanslist) {
			// TODO Auto-generated constructor stub
			this.context = context;
			this.goodsBeanslist = goodsBeanslist;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return goodsBeanslist == null ? 0 : goodsBeanslist.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			viewHolder holder = null;
			if (convertView == null) {
				holder = new viewHolder();
				convertView = LayoutInflater.from(context).inflate(
						R.layout.item_search_result, null);
				holder.search_result = (TextView) convertView
						.findViewById(R.id.search_result);
				convertView.setTag(holder);
			} else {
				holder = (viewHolder) convertView.getTag();
			}
			holder.search_result
					.setText(goodsBeanslist.get(position).getName());
			return convertView;
		}

	}

	class viewHolder {
		TextView search_result;
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == GoodsPresenter.Fenye_success) {
				goodsBeanslist.clear();
				goodsBean = (GoodsBean) entity.getData();

				goodsBeanslist.addAll(goodsBean.getList());
				if (goodsBean == null || "0".equals(goodsBean.getCount())) {
					null_search.setVisibility(View.VISIBLE);
					list.setVisibility(View.GONE);
				} else {
					null_search.setVisibility(View.GONE);
					list.setVisibility(View.VISIBLE);
				}
				adapter = new MyTypeDetailAdapter(ActivityTypeDetail.this,
						goodsBeanslist);
				list.setAdapter(adapter);
				list.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(ActivityTypeDetail.this,
								GoodsDetail.class);
						intent.putExtra("pid", goodsBeanslist.get(arg2).getId());
						startActivity(intent);
					}
				});
				try {
					closeProgressDialog();
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		}
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub
		
	}
}
